package com.hanson.controller;

import com.hanson.common.bean.CartParam;
import com.hanson.common.bean.OrderSubmitParam;
import com.hanson.common.bean.ResultMessage;
import com.hanson.entity.GroupGoodsBean;
import com.hanson.entity.OrderBean;
import com.hanson.entity.SeckillGoodsBean;
import com.hanson.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/web/order")
public class WebOrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(@RequestBody OrderBean orderBean, @CookieValue("openId") String openId) throws Exception {
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setData(orderService.addOrder(openId, orderBean));
        resultMessage.setMsg("新增订单成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/addSkillOrder", method = RequestMethod.POST)
    public ResultMessage addSkillOrder(@RequestBody SeckillGoodsBean seckillGoodsBean, @CookieValue("openId") String openId) throws Exception {
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setData(orderService.addSkillOrder(openId, seckillGoodsBean));
        resultMessage.setMsg("新增订单成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/addGroupOrder", method = RequestMethod.POST)
    public ResultMessage addGroupOrder(@RequestBody GroupGoodsBean groupGoodsBean, @CookieValue("openId") String openId) throws Exception {
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setData(orderService.addGroupOrder(openId, groupGoodsBean));
        resultMessage.setMsg("新增订单成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/addCartOrder", method = RequestMethod.POST)
    public ResultMessage addCartOrder(@RequestBody CartParam cartParam, @CookieValue("openId") String openId) throws Exception {
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setData(orderService.addCartOrder(openId, cartParam));
        resultMessage.setMsg("新增订单成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/updateExtInfo", method = RequestMethod.POST)
    public ResultMessage updateExtInfo(@RequestBody OrderSubmitParam orderSubmitParam, @CookieValue("openId") String openId) throws Exception {
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setData(orderService.updateAddressInfo(orderSubmitParam,openId));
        resultMessage.setMsg("修改地址信息成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete/{orderSeq}", method = RequestMethod.POST)
    public ResultMessage delete(@PathVariable String orderSeq) throws Exception {
        orderService.deleteOrder(orderSeq);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("操作成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/confirm/{orderSeq}", method = RequestMethod.POST)
    public ResultMessage confirm(@PathVariable String orderSeq) throws Exception {
        orderService.updateStatus(4,orderSeq);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("操作成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/finsish/{orderSeq}", method = RequestMethod.POST)
    public ResultMessage finsish(@PathVariable String orderSeq) throws Exception {
        orderService.finishOrder(orderSeq);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("操作成功！");
        return resultMessage;
    }
}
